package com.controller;


import com.annotation.IgnoreAuth;
import com.service.EmailService;
import com.utils.R;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 邮箱服务控制类
 * */
@RestController
@RequestMapping("/api/email")
public class EmailController {

    private final EmailService emailService; // 依赖于接口

    public EmailController(EmailService emailService) {
        this.emailService = emailService;
    }

    /**
     * 请求发送验证码
     * @param email 用户邮箱
     * @return 响应消息
     */
    @IgnoreAuth
    @PostMapping("/send-verification-code")
    public R sendVerificationCode(@RequestParam String email) {
        if (emailService.sendVerificationCode(email)) {
            return R.ok("验证码已发送到您的邮箱。请在5分钟内完成验证。");
        } else {
            return R.error("发送验证码失败，请检查邮箱地址或稍后再试。");
        }
    }

    /**
     * 验证验证码
     * @param email 用户邮箱
     * @param code 用户输入的验证码
     * @return 响应消息
     */
    @IgnoreAuth
    @PostMapping("/verify-code")
    public R verifyCode(@RequestParam String email, @RequestParam String code) {
        if (emailService.verifyCode(email, code)) {
            // 验证成功后，这里可以进行用户注册逻辑
            // 例如：userRegistrationService.registerUser(email, password);
            return R.ok("验证成功，可以进行注册。");
        } else {
            return R.error("验证码错误或已过期。");
        }
    }
}
